﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Security;
using System.IO;

namespace monitor_2_DBwork
{
    public class ClientsChecker : LicHelper
    {
        private const string Chkstr = "don'tbrake_it";
        private static Guid _serverId = new Guid("3A56C320-62E0-4D9E-80F1-86B24C7A95A1");
        //private Guid _clientId = new Guid("E02C0C73-5A7E-41AC-B637-54BC8B5ACBE5");
        private byte[] _vect;
        private readonly Aes _aes = new AesCryptoServiceProvider();
        private List<byte[]> _clientList;
        private bool _initialized = false;
        //private const string Chk = "E02C0C73-5A7E-41AC-B637-54BC8B5ACBE5";
        
        public string Init()
        {
            if (_initialized)
                return "ok";
            const string dir = "Resources\\ClientKeys";
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            var files = Directory.GetFiles(dir);
            _clientList = new List<byte[]>(files.Length);
            if(files.Length > 0)
                foreach (var file in files)
                {
                    if(file.EndsWith(".pbl"))
                        _clientList.Add(File.ReadAllBytes(file));
                }
            _initialized = true;
            return "ok";

        }

        public void Reset()
        {
            _initialized = false;
        }

        //СДЕЛАТЬ
        public bool CheckClient(byte[] chkArray)
        {
            if (!_initialized)
                if (Init() != "ok")
                    return false;
            //var mas = _clientId.ToByteArray();
            //var mas1 = Encoding.ASCII.GetBytes(Chkstr);
            
            //var clid = new byte[mas.Length + mas1.Length];

            //Buffer.BlockCopy(mas, 0, clid, 0, mas.Length);
            //Buffer.BlockCopy(mas1, 0, clid, mas.Length, mas1.Length);
            //------------------------------------------------------------
            //var s = _clientId.ToString();
            //var fs = Chk + Chkstr;
            //var clid = Encoding.UTF8.GetBytes(fs);
            //var a = Encrypt(clid);

            //var sb = new StringBuilder(a.Length);

            //foreach (var a1 in a)
            //{
            //    sb.Append(a1 + "-");
            //}
            byte[] b;
            try
            {
                b = Decrypt(chkArray, _serverId.ToByteArray());
            }
            catch (Exception)
            {
                
                throw;
            }
            
            var str = Encoding.ASCII.GetString(b);
            if (!str.EndsWith(Chkstr))
                return false;
            var clid = new byte[b.Length - Chkstr.Length];
            Buffer.BlockCopy(b, 0, clid, 0, clid.Length);
            byte[] h = GetHash(clid);

            if (_clientList.Count < 1)
                return false;
            return _clientList.Any(client => CompareByteArrays(client, h));

            //return CompareByteArrays(clid,_clientId.ToByteArray());
            //return true;
        }

        public static void CreateLicFile(string guid, string path)
        {
            CreateLicFile(Guid.Parse(guid), path);
        }

        public static void CreateLicFile(Guid guid, string path)
        {
            CreateLicFile(guid.ToByteArray(), path);
        }

        public static void CreateLicFile(byte[] guid, string path)
        {
            byte[] b = GetHash(guid);
            File.WriteAllBytes(path + ".pbl", b);
        }

        public static byte[] CreateForIvan(byte[] mas)
        {
            var mas1 = Encoding.ASCII.GetBytes(Chkstr);

            var clid = new byte[mas.Length + mas1.Length];

            Buffer.BlockCopy(mas, 0, clid, 0, mas.Length);
            Buffer.BlockCopy(mas1, 0, clid, mas.Length, mas1.Length);
            //------------------------------------------------------------
            //var s = _clientId.ToString();
            //var fs = Chk + Chkstr;
            //var clid = Encoding.UTF8.GetBytes(fs);
            var a = Encrypt(clid, _serverId.ToByteArray());

            return a;

            //var sb = new StringBuilder(a.Length);

            //foreach (var a1 in a)
            //{
            //    sb.Append(a1 + "-");
            //}
            //return sb.Remove(sb.Length - 1, 1).ToString();
        }

        //private byte[] Encrypt(byte[] toEncrypt)
        //{
        //    var enc = _aes.CreateEncryptor(_serverId.ToByteArray(), _serverId.ToByteArray());
        //    var a = enc.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
        //    return a;
        //}

        //private byte[] Decrypt(byte[] toDecrypt)
        //{
        //    var dec = _aes.CreateDecryptor(_serverId.ToByteArray(), _serverId.ToByteArray());
        //    var a = dec.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length);
        //    return a;
        //}

    }
}
